home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.text;
-
- import com.sun.java.swing.event.DocumentEvent;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import java.awt.Shape;
-
- public class PlainView extends View implements TabExpander {
- protected FontMetrics metrics;
- Segment lineBuffer = new Segment();
- int width;
- int tabSize;
- int tabBase;
- JTextComponent host;
- int sel0;
- int sel1;
- Color unselected;
- Color selected;
-
- public PlainView(Element elem) {
- super(elem);
- }
-
- public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
- this.updateDamage(changes, a, f);
- }
-
- private void damageLineRange(int line0, int line1, Shape a, Component host) {
- if (a != null) {
- Rectangle area0 = this.lineToRect(a, line0);
- Rectangle area1 = this.lineToRect(a, line1);
- if (area0 != null && area1 != null) {
- Rectangle damage = area0.union(area1);
- host.repaint(damage.x, damage.y, damage.width, damage.height);
- } else {
- host.repaint();
- }
- }
-
- }
-
- protected void drawLine(int lineIndex, Graphics g, int x, int y) {
- try {
- Element line = ((View)this).getElement().getElement(lineIndex);
- int p0 = line.getStartOffset();
- int p1 = line.getEndOffset();
- p1 = Math.min(((View)this).getDocument().getLength(), p1);
- if (this.sel0 == this.sel1) {
- this.drawUnselectedText(g, x, y, p0, p1);
- } else if (p0 >= this.sel0 && p0 <= this.sel1 && p1 >= this.sel0 && p1 <= this.sel1) {
- this.drawSelectedText(g, x, y, p0, p1);
- } else if (this.sel0 >= p0 && this.sel0 <= p1) {
- if (this.sel1 >= p0 && this.sel1 <= p1) {
- x = this.drawUnselectedText(g, x, y, p0, this.sel0);
- x = this.drawSelectedText(g, x, y, this.sel0, this.sel1);
- this.drawUnselectedText(g, x, y, this.sel1, p1);
- } else {
- x = this.drawUnselectedText(g, x, y, p0, this.sel0);
- this.drawSelectedText(g, x, y, this.sel0, p1);
- }
- } else if (this.sel1 >= p0 && this.sel1 <= p1) {
- x = this.drawSelectedText(g, x, y, p0, this.sel1);
- this.drawUnselectedText(g, x, y, this.sel1, p1);
- } else {
- this.drawUnselectedText(g, x, y, p0, p1);
- }
-
- } catch (BadLocationException var8) {
- throw new StateInvariantError("Can't render line: " + lineIndex);
- }
- }
-
- protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1) throws BadLocationException {
- g.setColor(this.selected);
- Document doc = ((View)this).getDocument();
- doc.getText(p0, p1 - p0, this.lineBuffer);
- return Utilities.drawTabbedText(this.lineBuffer, x, y, g, this, p0);
- }
-
- protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1) throws BadLocationException {
- g.setColor(this.unselected);
- Document doc = ((View)this).getDocument();
- doc.getText(p0, p1 - p0, this.lineBuffer);
- return Utilities.drawTabbedText(this.lineBuffer, x, y, g, this, p0);
- }
-
- protected final Segment getLineBuffer() {
- return this.lineBuffer;
- }
-
- protected int getLineLimit() {
- Integer lineLimit = (Integer)((View)this).getDocument().getProperty("lineLimit");
- if (lineLimit == null) {
- int width = 0;
- int totalLines = ((View)this).getElement().getElementCount();
-
- for(int i = 0; i < totalLines; ++i) {
- Element line = ((View)this).getElement().getElement(i);
- int p0 = line.getStartOffset();
- int p1 = line.getEndOffset();
- if (p1 - p0 > width) {
- width = p1 - p0;
- }
- }
-
- lineLimit = new Integer(width);
- ((View)this).getDocument().putProperty("lineLimit", lineLimit);
- }
-
- return lineLimit;
- }
-
- public float getPreferredSpan(int axis) {
- this.updateMetrics();
- switch (axis) {
- case 0:
- return (float)this.width;
- case 1:
- return (float)(((View)this).getElement().getElementCount() * this.metrics.getHeight());
- default:
- throw new IllegalArgumentException("Invalid axis: " + axis);
- }
- }
-
- protected int getTabSize() {
- Integer i = (Integer)((View)this).getDocument().getProperty("tabSize");
- int size = i != null ? i : 8;
- return size;
- }
-
- public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
- this.updateDamage(changes, a, f);
- }
-
- private Rectangle lineToRect(Shape a, int line) {
- Rectangle r = null;
- if (this.metrics != null) {
- Rectangle alloc = a.getBounds();
- r = new Rectangle(alloc.x, alloc.y + line * this.metrics.getHeight(), alloc.width, this.metrics.getHeight());
- }
-
- return r;
- }
-
- public Shape modelToView(int pos, Shape a) throws BadLocationException {
- Document doc = ((View)this).getDocument();
- Element map = ((View)this).getElement();
- int lineIndex = map.getElementIndex(pos);
- Rectangle lineArea = this.lineToRect(a, lineIndex);
- this.tabBase = lineArea.x;
- Element line = map.getElement(lineIndex);
- int p0 = line.getStartOffset();
- doc.getText(p0, pos - p0, this.lineBuffer);
- int xOffs = Utilities.getTabbedTextWidth(this.lineBuffer, this.metrics, this.tabBase, this, p0);
- lineArea.x += xOffs;
- lineArea.width = 1;
- lineArea.height = this.metrics.getHeight();
- return lineArea;
- }
-
- public float nextTabStop(float x, int tabOffset) {
- int ntabs = ((int)x - this.tabBase) / this.tabSize;
- return (float)(this.tabBase + (ntabs + 1) * this.tabSize);
- }
-
- public void paint(Graphics g, Shape a) {
- Rectangle alloc = (Rectangle)a;
- this.tabBase = alloc.x;
- g.setFont(this.host.getFont());
- this.sel0 = this.host.getSelectionStart();
- this.sel1 = this.host.getSelectionEnd();
- this.unselected = this.host.isEnabled() ? this.host.getForeground() : this.host.getDisabledTextColor();
- Caret c = this.host.getCaret();
- this.selected = c.isSelectionVisible() ? this.host.getSelectedTextColor() : this.unselected;
- this.updateMetrics();
- Rectangle clip = g.getClipBounds();
- int fontHeight = this.metrics.getHeight();
- int heightBelow = alloc.y + alloc.height - (clip.y + clip.height);
- int linesBelow = Math.max(0, heightBelow / fontHeight);
- int heightAbove = clip.y - alloc.y;
- int linesAbove = Math.max(0, heightAbove / fontHeight);
- int linesTotal = alloc.height / fontHeight;
- Rectangle lineArea = this.lineToRect(a, linesAbove);
- int y = lineArea.y + this.metrics.getAscent();
- int x = lineArea.x;
- Element map = ((View)this).getElement();
- int endLine = Math.min(map.getElementCount(), linesTotal - linesBelow);
-
- for(int line = linesAbove; line < endLine; ++line) {
- this.drawLine(line, g, x, y);
- y += fontHeight;
- }
-
- }
-
- public void preferenceChanged(View child, boolean width, boolean height) {
- ((View)this).getDocument().putProperty("lineLimit", (Object)null);
- super.preferenceChanged(child, width, height);
- }
-
- public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
- this.updateDamage(changes, a, f);
- }
-
- public void setParent(View p) {
- super.setParent(p);
- this.host = (JTextComponent)((View)this).getContainer();
- }
-
- void updateDamage(DocumentEvent changes, Shape a, ViewFactory f) {
- if (this.host.isShowing()) {
- this.updateMetrics();
- Element elem = ((View)this).getElement();
- DocumentEvent.ElementChange ec = changes.getChange(elem);
- Element[] added = ec != null ? ec.getChildrenAdded() : null;
- Element[] removed = ec != null ? ec.getChildrenRemoved() : null;
- if ((added == null || added.length <= 0) && (removed == null || removed.length <= 0)) {
- this.preferenceChanged((View)null, true, false);
- Element map = ((View)this).getElement();
- int line = map.getElementIndex(changes.getOffset());
- this.damageLineRange(line, line, a, this.host);
- } else {
- this.preferenceChanged((View)null, true, true);
- this.host.repaint();
- }
- }
-
- }
-
- final void updateMetrics() {
- Component host = ((View)this).getContainer();
- Font f = host.getFont();
- this.metrics = host.getFontMetrics(f);
- int columnWidth = this.metrics.charWidth('m');
- this.width = this.getLineLimit() * columnWidth;
- this.tabSize = this.getTabSize() * columnWidth;
- }
-
- public int viewToModel(float fx, float fy, Shape a) {
- Rectangle alloc = a.getBounds();
- Document doc = ((View)this).getDocument();
- int x = (int)fx;
- int y = (int)fy;
- if (y < alloc.y) {
- return ((View)this).getStartOffset();
- } else if (y > alloc.y + alloc.height) {
- return ((View)this).getEndOffset() - 1;
- } else {
- Element map = doc.getDefaultRootElement();
- int lineIndex = Math.abs((y - alloc.y) / this.metrics.getHeight());
- if (lineIndex >= map.getElementCount()) {
- return ((View)this).getEndOffset() - 1;
- } else {
- Element line = map.getElement(lineIndex);
- if (x < alloc.x) {
- return line.getStartOffset();
- } else if (x > alloc.x + alloc.width) {
- return line.getEndOffset() - 1;
- } else {
- try {
- int p0 = line.getStartOffset();
- int p1 = line.getEndOffset() - 1;
- doc.getText(p0, p1 - p0, this.lineBuffer);
- this.tabBase = alloc.x;
- int offs = p0 + Utilities.getTabbedTextOffset(this.lineBuffer, this.metrics, this.tabBase, x, this, p0);
- return offs;
- } catch (BadLocationException var14) {
- return -1;
- }
- }
- }
- }
- }
- }
-